package juez

class SubmitionController {

    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]
       /**intercepta la conexion llamando primero al metodo auth**/
    def beforeInterceptor = [action:this.&auth]

    def index = {
        redirect(action: "list", params: params)
    }
    
     /**impide el paso a menos que este logeado**/
      def auth() {
        if(!session.user) {
            redirect(controller:"user", action:"login")
            return false
        }
    }
    /**impide el paso a menos que este logeado**/

    def list = {
        params.max = Math.min(params.max ? params.int('max') : 10, 100)
        [submitionInstanceList: Submition.list(params), submitionInstanceTotal: Submition.count()]
    }

    def create = {
        def submitionInstance = new Submition()
        submitionInstance.properties = params
        return [submitionInstance: submitionInstance]
    }

    def save = {
        def submitionInstance = new Submition(params)
        if (submitionInstance.save(flush: true)) {
            flash.message = "${message(code: 'default.created.message', args: [message(code: 'submition.label', default: 'Submition'), submitionInstance.id])}"
            redirect(action: "show", id: submitionInstance.id)
        }
        else {
            render(view: "create", model: [submitionInstance: submitionInstance])
        }
    }

    def show = {
        def submitionInstance = Submition.get(params.id)
        if (!submitionInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'submition.label', default: 'Submition'), params.id])}"
            redirect(action: "list")
        }
        else {
            [submitionInstance: submitionInstance]
        }
    }

    def edit = {
        def submitionInstance = Submition.get(params.id)
        if (!submitionInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'submition.label', default: 'Submition'), params.id])}"
            redirect(action: "list")
        }
        else {
            return [submitionInstance: submitionInstance]
        }
    }

    def update = {
        def submitionInstance = Submition.get(params.id)
        if (submitionInstance) {
            if (params.version) {
                def version = params.version.toLong()
                if (submitionInstance.version > version) {
                    
                    submitionInstance.errors.rejectValue("version", "default.optimistic.locking.failure", [message(code: 'submition.label', default: 'Submition')] as Object[], "Another user has updated this Submition while you were editing")
                    render(view: "edit", model: [submitionInstance: submitionInstance])
                    return
                }
            }
            submitionInstance.properties = params
            if (!submitionInstance.hasErrors() && submitionInstance.save(flush: true)) {
                flash.message = "${message(code: 'default.updated.message', args: [message(code: 'submition.label', default: 'Submition'), submitionInstance.id])}"
                redirect(action: "show", id: submitionInstance.id)
            }
            else {
                render(view: "edit", model: [submitionInstance: submitionInstance])
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'submition.label', default: 'Submition'), params.id])}"
            redirect(action: "list")
        }
    }

    def delete = {
        def submitionInstance = Submition.get(params.id)
        if (submitionInstance) {
            try {
                submitionInstance.delete(flush: true)
                flash.message = "${message(code: 'default.deleted.message', args: [message(code: 'submition.label', default: 'Submition'), params.id])}"
                redirect(action: "list")
            }
            catch (org.springframework.dao.DataIntegrityViolationException e) {
                flash.message = "${message(code: 'default.not.deleted.message', args: [message(code: 'submition.label', default: 'Submition'), params.id])}"
                redirect(action: "show", id: params.id)
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'submition.label', default: 'Submition'), params.id])}"
            redirect(action: "list")
        }
    }
}
